﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace SalomonWebsite
{
    public partial class ProDetail : System.Web.UI.Page
    {
        public string strCon = Global.Connection;
        public string ImageWeb = Global.ImageWeb;
        public int ParPKID;
        public int PKID;
        public string Val = "";

        public string proName = "";
        public string description = "";
        protected string proContent = "";
        protected string imgURL = "";
        protected int serId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["parid"] != null && Request["parid"] != "" && Request["pkid"] != null && Request["pkid"] != "" && Request["val"] != null && Request["val"] != "")
                {
                    ParPKID = Convert.ToInt32(Request["parid"]);
                    PKID = Convert.ToInt32(Request["pkid"]);
                    Val = Request["val"].ToString();

                    DataTable dtPro = new DataTable();
                    dtPro = Bll.Product.Single(strCon, PKID);
                    if (dtPro.Rows.Count == 1)
                    {
                        DataRow[] drs = dtPro.Select(" [ProCode] = '" + Val + "'");
                        if (drs.Length == 1)
                        {
                            proName = drs[0]["ProName"].ToString();
                            description = drs[0]["Description"].ToString();
                            proContent = drs[0]["ProContent"].ToString();
                            imgURL = drs[0]["ImgURL"].ToString();
                            serId = Convert.ToInt32(drs[0]["SerID"].ToString());

                            this.PageNote.PKID = Convert.ToInt32(drs[0]["CatID"]);
                            this.PageNote.ParPKID = ParPKID;
                            this.CatAd.ParPKID = ParPKID;

                            DataTable dtCat = new DataTable();
                            dtCat = Bll.Category.Single(strCon, Convert.ToInt32(drs[0]["CatID"]));
                            if (dtCat.Rows.Count == 1)
                            {
                                this.CatTree.PKID = Convert.ToInt32(dtCat.Rows[0]["PKID"]);
                                this.CatTree.ParPKID = ParPKID;
                                this.CatTree.FarPKID = Convert.ToInt32(dtCat.Rows[0]["ParID"]);
                            }
                            dtCat.Dispose();
                            

                            
                        }
                        else
                        {
                            Response.Redirect("FileNotFound.aspx");
                        }
                        dtPro.Dispose();
                    }
                    else
                    {
                        Response.Redirect("FileNotFound.aspx");
                    }
                }
                else
                {
                    Response.Redirect("FileNotFound.aspx");
                }
            }
        }
        
        //protected string proName = "";
        //protected string description = "";
        //protected string proContent = "";
        //protected string imgURL = "";
        //protected string strCon = Global.Connection;
        //protected int PKID = 0;
        //protected int catId = 0;
        //protected int serId = 0;
        //protected string nowName = "";
        //public string ImageWeb = Global.ImageWeb;

        //public int secordNowPKID = 0;
        //public string secordNowCatName = "";
        //public string secordNowImgURL="";
        //public int secordNowParID = 0;
        //public int secordNowCatRank = 0;
        //public string secordNowDescription = "";
        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    if(!IsPostBack)
        //    {
        //        if (Request["val"] != null && Request["val"] != "" && Request["pkid"] != null && Request["pkid"] != "") 
        //        {
        //            string Val = Request["val"].ToString();
        //            PKID = Convert.ToInt32(Request["pkid"]);
        //            GetProductByPKID(strCon, PKID);
        //            GetProSecordCat(strCon, catId);
        //            GetLeftTemp(strCon, 1);  //二级分类
        //            GetNowCatByPKID(strCon, secordNowPKID);
        //            GetLeft(strCon);
        //        }
        //    }
        //}

        //protected void GetProductByPKID(string strCon,int pkid) 
        //{
        //    DataTable dt = new DataTable();
        //    dt = Bll.Product.Single(strCon, pkid);
        //    if (dt != null && dt.Rows.Count == 1) 
        //    {
        //        proName = dt.Rows[0]["ProName"].ToString();
        //        description = dt.Rows[0]["Description"].ToString();
        //        proContent = dt.Rows[0]["ProContent"].ToString();
        //        imgURL = dt.Rows[0]["ImgURL"].ToString();
        //        serId = Convert.ToInt32(dt.Rows[0]["SerID"].ToString());
        //    }
        //}

        //protected void GetProSecordCat(string strCon, int id) 
        //{
        //    DataTable dt = Bll.Category.Single(strCon, id);
        //    if (dt != null && dt.Rows.Count > 0) 
        //    {
        //        secordNowPKID = Convert.ToInt32(dt.Rows[0]["ParID"].ToString());
        //        nowName = dt.Rows[0]["CatName"].ToString();
        //    }
        //}
        ///// <summary>
        ///// 找出对应id的级别分类
        ///// </summary>
        ///// <param name="strCon">连接字符串</param>
        ///// <param name="id">级别Id</param>
        ///// <returns>该Id对应的分类</returns>
        //protected DataTable GetLeftTemp(string strCon, int id)
        //{
        //    DataTable dtLeft = Bll.Category.MULTIPLE(strCon, id);    
        //    return dtLeft;
        //}

        ///// <summary>
        ///// 当前产品所属的二级分类
        ///// </summary>
        ///// <param name="strCon">连接字符串</param>
        ///// <param name="PKID">当前产品所属的二级分类Id</param>
        //protected void GetNowCatByPKID(string strCon, int PKID)
        //{
        //    DataTable dt = Bll.Category.Single(strCon, PKID);
        //    //secordNowPKID = Convert.ToInt32(dt.Rows[0]["PKID"].ToString());
        //    secordNowCatName = dt.Rows[0]["CatName"].ToString();
        //    secordNowImgURL = dt.Rows[0]["ImgURL"].ToString();
        //    secordNowParID = Convert.ToInt32(dt.Rows[0]["ParID"].ToString());
        //    secordNowCatRank = Convert.ToInt32(dt.Rows[0]["CatRank"].ToString());
        //    secordNowDescription = dt.Rows[0]["Description"].ToString();
        //}
        ///// <summary>
        ///// 创建表
        ///// </summary>
        ///// <returns></returns>
        //protected DataTable CreateTableLeft()
        //{
        //    DataTable dt = new DataTable();
        //    dt.Columns.Add("PKID", typeof(Int32));
        //    dt.Columns.Add("CatName", typeof(string));
        //    dt.Columns.Add("ImgURL", typeof(string));
        //    dt.Columns.Add("ParID", typeof(Int32));
        //    dt.Columns.Add("CatRank", typeof(Int32));
        //    dt.Columns.Add("Description", typeof(string));
        //    return dt;
        //}

        //protected void GetLeft(string strCon)
        //{
        //    int index = 0;
        //    DataTable dt = GetLeftTemp(strCon, 1);
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        if (Convert.ToInt32(dt.Rows[i]["PKID"].ToString()) == secordNowPKID)
        //        {
        //            index = i;
        //            break;
        //        }
        //    }
        //    dt.Rows.Remove(dt.Rows[index]);
        //    DataTable dtLeft = CreateTableLeft();
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        for (int i = 0; i <= dt.Rows.Count; i++)
        //        {
        //            DataRow newRow = dtLeft.NewRow();
        //            if (i == 0)
        //            {
        //                newRow["PKID"] = Convert.ToInt32(dt.Rows[i]["PKID"].ToString());
        //                newRow["CatName"] = dt.Rows[i]["CatName"].ToString();
        //                newRow["ImgURL"] = dt.Rows[i]["ImgURL"].ToString();
        //                newRow["ParID"] = Convert.ToInt32(dt.Rows[i]["ParID"].ToString());
        //                newRow["CatRank"] = Convert.ToInt32(dt.Rows[i]["CatRank"].ToString());
        //                newRow["Description"] = dt.Rows[i]["Description"].ToString();
        //            }
        //            else if (i == 1)
        //            {
        //                newRow["PKID"] = secordNowPKID;
        //                newRow["CatName"] = secordNowCatName;
        //                newRow["ImgURL"] = secordNowImgURL;
        //                newRow["ParID"] = secordNowParID;
        //                newRow["CatRank"] = secordNowCatRank;
        //                newRow["Description"] = secordNowDescription;
        //            }
        //            else if (i >= 2)
        //            {
        //                newRow["PKID"] = Convert.ToInt32(dt.Rows[i - 1]["PKID"].ToString());
        //                newRow["CatName"] = dt.Rows[i - 1]["CatName"].ToString();
        //                newRow["ImgURL"] = dt.Rows[i - 1]["ImgURL"].ToString();
        //                newRow["ParID"] = Convert.ToInt32(dt.Rows[i - 1]["ParID"].ToString());
        //                newRow["CatRank"] = Convert.ToInt32(dt.Rows[i - 1]["CatRank"].ToString());
        //                newRow["Description"] = dt.Rows[i - 1]["Description"].ToString();
        //            }
        //            dtLeft.Rows.Add(newRow);
        //        }
        //    }
        //    this.repLeft.DataSource = dtLeft.DefaultView;
        //    this.repLeft.DataBind();
        //    repLeft.Dispose();
        //}

        ////二级分类
        //protected void repLeft_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        Repeater rep = (Repeater)e.Item.FindControl("repLeftList");
        //        //找到分类repeater关联的数据项 
        //        DataRowView drv = (DataRowView)e.Item.DataItem;
        //        //提取分类id 
        //        int serId = Convert.ToInt32(drv["PKID"]);
        //        //根据分类id查询该分类下的产品，并绑定产品repeater 
        //        rep.DataSource = GetLeftTemp(strCon, serId);
        //        rep.DataBind();
        //    }
        //}

    }
}
